home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Microsoft Visual Basic .NET
/
Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin
/
11 object serialization
/
xmlserializationdemo
/
classes.vb
< prev
next >
Wrap
Text File
|
2002-03-16
|
4KB
|
159 lines
Imports System.Xml.Serialization
' a simple class for our serialization experiments
Public Class Customer
Public ID As Integer
Public Name As String
Public Address As String
Public City As String
' All XML-serializable classes must support a parameterless constructor.
Sub New()
' nothing to do in this example.
End Sub
Sub New(ByVal id As Integer, ByVal name As String, ByVal address As String, ByVal city As String)
Me.ID = id
Me.Name = name
Me.Address = address
Me.City = city
End Sub
End Class
' a serializable class with attributes
<XmlRootAttribute("Customer", Namespace:="http://www.vb2themax.com", IsNullable:=False)> _
Public Class Customer2
<XmlAttributeAttribute("CustId")> Public ID As Integer
<XmlElement("name")> Public Name As String
<XmlIgnore()> Public Address As String
<XmlElement("city", IsNullable:=False)> Public City As String
<XmlText()> Public Notes As String
' All XML-serializable classes must support a parameterless constructor.
Sub New()
' nothing to do in this example.
End Sub
Sub New(ByVal id As Integer, ByVal name As String, ByVal address As String, ByVal city As String)
Me.ID = id
Me.Name = name
Me.Address = address
Me.City = city
End Sub
End Class
' another serializable class with attributes
Public Class Customer3
<XmlAttributeAttribute("CustId", Namespace:="www.abc.com")> _
Public ID As Integer
<XmlElement("name", Namespace:="www.abc.com")> _
Public Name As String
<XmlIgnore()> _
Public Address As String
<XmlElement("city", IsNullable:=False)> _
Public City As String
<XmlText()> _
Public Notes As String
' All XML-serializable classes must support a parameterless constructor.
Sub New()
' nothing to do in this example.
End Sub
Sub New(ByVal id As Integer, ByVal name As String, ByVal address As String, ByVal city As String)
Me.ID = id
Me.Name = name
Me.Address = address
Me.City = city
End Sub
End Class
' a Customer class that contains nested Order objects.
Public Class Customer4
<XmlArray("CustOrders"), XmlArrayItem("CustOrder", IsNullable:=True)> _
Public Orders(3) As Order
<XmlAttributeAttribute("CustId", Namespace:="www.abc.com")> _
Public ID As Integer
<XmlElement("name", Namespace:="www.abc.com")> _
Public Name As String
<XmlIgnore()> _
Public Address As String
<XmlElement("city", IsNullable:=False)> _
Public City As String
<XmlText()> _
Public Notes As String
' All XML-serializable classes must support a parameterless constructor.
Sub New()
' nothing to do in this example.
End Sub
Sub New(ByVal id As Integer, ByVal name As String, ByVal address As String, ByVal city As String)
Me.ID = id
Me.Name = name
Me.Address = address
Me.City = city
End Sub
End Class
Public Class Order
<XmlAttributeAttribute("OrderId")> _
Public ID As Integer
Public [Date] As Date ' Note how we deal with a VB reserved word.
Public Total As Decimal
' All XML serializable classes must have a default constructor.
Sub New()
' Nothing to do in this demo
End Sub
Sub New(ByVal id As Integer, ByVal [date] As Date, ByVal total As Decimal)
Me.ID = id
Me.Date = [date]
Me.Total = total
End Sub
End Class
' these classes are used to explain XML serialization overriding
Public Class AddressBook
Public Name As String
Public Contacts() As Person
End Class
Public Class Person
Public ID As Integer
Public Name As String
Public PhoneNumber As String
End Class
Public Class Employee
Inherits Person
Public SSN As String
End Class
Public Class CandidateEmployee
Inherits Person
Public InterviewDate As Date
End Class